#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @File   : e_multiprocessing.py
# @Email  : xuexia_xu@feidee.com
# @Time   : 2024/4/2 14:48
# @Desc   : 多进程
import time
from multiprocessing import Process
import os

# print('Process (%s) start ...' % os.getpid())
if os.name != 'nt':
    # Only works on Unix/Linux/Mac: windows下可以使用multiproessing代替os.fork功能
    pid = os.fork()
    if pid == 0:
        print('I am child process (%s) and my parent is %s.' % (os.getpid(), os.getppid()))
    else:
        print('I (%s) just created a child process (%s).' % (os.getpid(), pid))


# 子进程要执行的代码
def run_proc(name):
    print('Run child process %s (%s)...' % (name, os.getpid()))


if __name__ == '__main__':
    print('Parent process %s.' % os.getpid())
    p = Process(target=run_proc, args=('test',))
    print('Child process will start.')
    p.start()  # 启动
    p.join()  # join()方法可以等待子进程结束后再继续往下运行，通常用于进程间的同步。
    print('Child process end.')
